package com.young.bs.vote.service.impl;

import com.young.bs.vote.service.IVotePersonService;
import com.young.bs.vote.service.IVoteResultService;
import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.bs.vote.model.VoteInfo;
import com.young.bs.vote.service.IVoteInfoService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.IdGenerator;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
* 投票主信息服务实现
* Created by admin on 2019-3-19.
*/
@Service("voteInfoService")
public class VoteInfoServiceImpl implements IVoteInfoService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    @Resource
    IVoteResultService voteResultService;

    private static final Logger logger = LoggerFactory.getLogger(VoteInfoServiceImpl.class);

    private static final String MAPPER = "com.young.bs.vote.mapper.voteInfo";//mapper的namespace

    //获取数据列表
    @Override
    public List<VoteInfo> getList(VoteInfo obj) {
        List<VoteInfo> list = dataAccessService.getList(MAPPER + ".getList", obj);
        if (list != null && !list.isEmpty()) {
            for (VoteInfo voteInfo : list) {
                voteInfo.refreshCanVoteFlag();
            }
        }
        return list;
    }
    @Resource(name="votePersonService")
    IVotePersonService votePersonService;

    /**
     * 查询可发起投票的信息
     *
     * @param obj
     * @return
     */
    @Override
    public List<VoteInfo> getCanVoteList(VoteInfo obj) {
        if (obj == null) {
            obj = new VoteInfo();
        }
        obj.setCurrentTime(new Date());
        obj.setTrashFlag(0);
        List<VoteInfo> list = dataAccessService.getList(MAPPER + ".getList", obj);
        if (list != null && !list.isEmpty()) {
            Iterator<VoteInfo> it = list.iterator();
            while (it.hasNext()) {
                VoteInfo voteInfo = it.next();
                //p判断用户是否有权限投票,没有则删除
                if (!votePersonService.canGoVote(obj.getId(), obj.getUserId())) {
                    it.remove();
                }
                voteInfo.refreshCanVoteFlag();
                //查询用户已经投票的结果信息
                voteInfo.setVoteResult(voteResultService.getByVoteIdAndUserId(voteInfo.getId(), obj.getUserId()));
            }
        }

        return list;
    }

    //获取数据列表(分页)
    @Override
    public List<VoteInfo> getPage(VoteInfo obj, Page page) {
        List<VoteInfo> list = dataAccessService.getList(MAPPER + ".getList", obj, page);
        if (list != null && !list.isEmpty()) {
            for (VoteInfo voteInfo : list) {
                voteInfo.refreshCanVoteFlag();
            }
        }
        return list;
    }

    //查询单条
    @Override
    public VoteInfo get(String id) {
        VoteInfo voteInfo = dataAccessService.getObject(MAPPER + ".get", id);
        if (voteInfo != null) {
            voteInfo.refreshCanVoteFlag();
        }
        return voteInfo;
    }

    /**
     * 查询详情
     *
     * @param id
     * @param userId
     * @return
     */
    @Override
    public VoteInfo getDetail(String id, String userId) {
        VoteInfo voteInfo = dataAccessService.getObject(MAPPER + ".get", id);
        if (voteInfo != null) {
            voteInfo.refreshCanVoteFlag();
            //查询用户已经投票的结果信息
            voteInfo.setVoteResult(voteResultService.getByVoteIdAndUserId(id, userId));
        }
        return voteInfo;
    }

    //统计数量
    @Override
    public int count(VoteInfo obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(VoteInfo obj) {
        obj.setUpdateTime(new Date());
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(VoteInfo obj) {
        obj.setId(CommonUtil.getUUID());
        obj.setCreateTime(new Date());
        obj.setUpdateTime(new Date());
        obj.setDataVersion(1);
        obj.setTrashFlag(0);
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<VoteInfo> list) {
        int i = 0;
        for(VoteInfo item : list){
            i += this.insert(item);
        }
        return i;
    }

}